PROJECT = "httpdemo"
VERSION = "1.0.0"

-- !字体设置：font_opposansm12_chinese
-- sys库是标配
_G.sys = require("sys")
--[[特别注意, 使用http库需要下列语句]]
_G.sysplus = require("sysplus")

---需要在云编译时添加SPI协议
spi_lcd = spi.deviceSetup(2, 7, 0, 0, 8, 40000000, spi.MSB, 1, 1) -- https://wiki.luatos.com/api/spi.html
-- spi.setup(id, cs, CPHA, CPOL, dataw, bandrate, bitdict, ms, mode)

lcd.init("st7789", {
    port = "device",
    pin_dc = 6,
    pin_pwr = 11,
    pin_rst = 10,
    direction = 0,
    w = 240,
    h = 320,
    xoffset = 0,
    yoffset = 0
}, spi_lcd)
-- 如果显示颜色相反，关闭反色
lcd.invoff()

sys.taskInit(function()

    sys.wait(1000)
    log.info(wlan.init())
    wlan.setMode(wlan.STATION)
    wlan.connect("ESP32", "776654321")

    ---等待wifi连接
    while not wlan.ready() do
        log.info("Disconnection")
        sys.wait(1000)
    end
    log.info("ready !!")

    lcd.setFont(lcd.font_opposansm12_chinese) -- 具体取值可参考api文档的常量表
    while 1 do

        ---1.http连接,code为响应代码,body是返回值
        local code_suggest, headers_suggest, body_suggest = http.request("GET",
                                                                   "https://api.seniverse.com/v3/life/suggestion.json?key=SeiP2ViJVtkngR1s5&location=luoyang&language=zh-Hans&days=2")
                                                          .wait() -- 留意末尾的.wait()

        ---2.因为body中有[]，无法对其进行table解析，只能先对body进行字符串操作，去除[]后在进行table解析
        --=http_body打印，类型为string
        --print(body_suggest)

        --=找到两天的date的位置，用于分割
        suggest_today_start, suggest_today_end = string.find(body_suggest, "date")
        suggest_tomorrow_start, suggest_tomorrow_end =string.find(body_suggest, "date", suggest_today_start+1)
        suggest_last_update_start, suggest_last_update_end =string.find(body_suggest, "last_update")
        --=验证位置
        -- print(suggest_today_start,suggest_today_end)
        -- print(suggest_tomorrow_start,suggest_tomorrow_end)
        -- print(suggest_last_update_start, suggest_last_update_end)
        --=验证两个table
        print(body_suggest:sub(suggest_today_start-2,suggest_tomorrow_start-4))
        print(body_suggest:sub(suggest_tomorrow_start-2,suggest_last_update_start-4))

        --!数组直接以{}的形式，不需要table名，直接转换赋值给API_weather_today_suggest，就为table
        local API_weather_today_suggest =json.decode(body_suggest:sub(suggest_today_start-2,suggest_tomorrow_start-4))
        local API_weather_tomorrow_suggest =json.decode(body_suggest:sub(suggest_tomorrow_start-2,suggest_last_update_start-4))

        print("空气质量："..API_weather_today_suggest.air_pollution.brief..","..API_weather_today_suggest.air_pollution.details)
        print("外面气候："..API_weather_today_suggest.comfort.brief..","..API_weather_today_suggest.comfort.details)
        print("今日穿衣："..API_weather_today_suggest.dressing.brief..","..API_weather_today_suggest.dressing.details)
        print("今日雨水："..API_weather_today_suggest.umbrella.brief..","..API_weather_today_suggest.umbrella.details)
        print("紫外线："..API_weather_today_suggest.uv.brief..","..API_weather_today_suggest.uv.details)

        print("   ")

        print("空气质量："..API_weather_tomorrow_suggest.air_pollution.brief..","..API_weather_tomorrow_suggest.air_pollution.details)
        print("外面气候："..API_weather_tomorrow_suggest.comfort.brief..","..API_weather_tomorrow_suggest.comfort.details)
        print("今日穿衣："..API_weather_tomorrow_suggest.dressing.brief..","..API_weather_tomorrow_suggest.dressing.details)
        print("今日雨水："..API_weather_tomorrow_suggest.umbrella.brief..","..API_weather_tomorrow_suggest.umbrella.details)
        print("紫外线："..API_weather_tomorrow_suggest.uv.brief..","..API_weather_today_suggest.uv.details)


        sys.wait(3000)
    end
end)

-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!
